package com.jwthhealth_pub.old.report.view.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.View;

import com.jwthhealth_pub.R;
import com.jwthhealth_pub.common.ScreenUtil;

/**
 * 柱形图
 * Created by liuzhen on 2017/10/16.
 */

public class JwthBarChart extends View {

//    private static String TAG = makeLogTag(JwthBarChart.class);

    private Context mContext;
    private float[] xAxisValues, yAxisValues;
    private float xMinValue;
    private float stepSize;// X轴最大值，最小值和步长
    private float columnStartPos;//柱状图起始位置， 即x轴值为0处
    private float mColumnHei; //柱状图高度
    private float mColumnInterval = 40f;//柱状图间间隙
    private Canvas mCanvas;
    private float mXInterval;
    private String[] mColumnColors;
    private int mPadding;
    private int[] warnLines;

    public void setWarnLines(int[] warnLines) {
        this.warnLines = warnLines;
    }

    public JwthBarChart(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;

        init();
    }

    private void init() {
        mColumnColors = mContext.getResources().getStringArray(R.array.column_colors);
        mColumnHei = ScreenUtil.dip2px(mContext, 20);
    }

    public void setxAxisValues(float _xMinValue, float _xMaxValue, float stepSize) {
        this.xMinValue = _xMinValue;
        this.stepSize = stepSize;

        if (xAxisValues == null)
            xAxisValues = new float[(int) ((_xMaxValue - xMinValue) / stepSize + 1)];

        if (xAxisValues.length > 0)
            xAxisValues[0] = xMinValue;

        for (int i = 1; i <= xAxisValues.length - 1; i++) {
            float _value = xAxisValues[i - 1];
            xAxisValues[i] = _value + stepSize;
        }

        invalidate();
    }

    public void setyAxisValues(float[] yAxisValues) {
        this.yAxisValues = yAxisValues;
//        invalidate();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(measureWidth(widthMeasureSpec),
                measureHeight(heightMeasureSpec));
    }

    private int measureHeight(int measureSpec) {
        int result;
        int mode = MeasureSpec.getMode(measureSpec);
        int size = MeasureSpec.getSize(measureSpec);

        if (mode == MeasureSpec.EXACTLY) {
            //柱与分割线位于顶层与底边的边距
            mPadding = 5;
            result = (int) ((int) mColumnHei + ((yAxisValues.length - 1) * (mColumnHei + mColumnInterval)) + 60 + mPadding);
//            result = (int) ((int) mColumnHei + ((yAxisValues.length - 1) * (mColumnHei + mColumnInterval)) + mPadding);
//            result = 500;
        } else {
            result = getWidth();
            if (mode == MeasureSpec.AT_MOST) {
                result = Math.min(result, size);
            }
        }
        return result;
    }

    private int measureWidth(int measureSpec) {
        int result;
        int mode = MeasureSpec.getMode(measureSpec);
        int size = MeasureSpec.getSize(measureSpec);

        if (mode == MeasureSpec.EXACTLY) {
            result = size;
        } else {
            result = 200;//根据自己的需要更改
            if (mode == MeasureSpec.AT_MOST) {
                result = Math.min(result, size);
            }
        }
        return result;

    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);

        mCanvas = canvas;

        //绘制间隔线
        drawSeptalLine();

        //绘制文字
        drawDescribeText();

        //绘制柱状图
        drawColumn(yAxisValues);
    }

    private void drawSeptalLine() {


        Paint linePaint = new Paint();

        if (xAxisValues == null || yAxisValues == null) return;
        // 横坐标间隔
        mXInterval = mCanvas.getWidth() / (xAxisValues.length + 1);


        for (int i = 0; i < xAxisValues.length; i++) {
            mCanvas.save();

            float _xPos = mXInterval * (i + 1);


//            int lineHei = (int) (mColumnHei + mColumnInterval) * (yAxisValues.length);
            int lineHei = (int) (mColumnHei + mColumnInterval) * (yAxisValues.length - 1) + (int) mColumnHei + 20;//todo 补了一段缺失的距离


            //虚线
//            int unitInterVal = lineHei / 15;
            int unitInterVal = 20;
            float[] pts;
            pts = new float[]{0, unitInterVal / 2, 0, unitInterVal};

            //警戒线默认只有两条
            if (warnLines != null) {
                if (i == warnLines[0] || i == warnLines[1]) {
                    pts = new float[]{0, 0, 0, unitInterVal};
                    linePaint.setColor(ContextCompat.getColor(mContext, R.color.warn_line));
                    linePaint.setStrokeWidth(2);
                } else {
                    pts = new float[]{0, unitInterVal / 2, 0, unitInterVal};
                    linePaint.setColor(Color.GRAY);
                    linePaint.setStrokeWidth(1);
                }
            }

//            mCanvas.translate(10 / 2, 0);
            int totalWidth = unitInterVal;
            mCanvas.translate(_xPos, 0);
            while (totalWidth <= lineHei) {
                mCanvas.drawLines(pts, linePaint);
                mCanvas.translate(0, unitInterVal);
                totalWidth += unitInterVal;
            }

            //实线
//            mCanvas.drawLine(_xPos, 0, _xPos, (int) mColumnHei + (int) (mColumnHei + mColumnInterval) * (yAxisValues.length - 1), linePaint);

            mCanvas.restore();

            if (xAxisValues[i] == 0) {
                columnStartPos = _xPos;
            }
        }
    }

    private void drawDescribeText() {
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStrokeWidth(8);
        paint.setTextSize(30);
        paint.setColor(ContextCompat.getColor(mContext, R.color.textColorGray));

        //该方法被调用了两次
        if (xAxisValues == null) return;

        for (int i = 0; i < xAxisValues.length; i++) {
            String _str = String.valueOf((int) (xMinValue + stepSize * i));
            float _xPos = mXInterval * (i + 1) - paint.measureText(_str) / 2;
            mCanvas.drawText(String.valueOf((int) (xMinValue + stepSize * i)), _xPos, (int) mColumnHei + (int) (mColumnHei + mColumnInterval) * (yAxisValues.length - 1) + 50 + mPadding, paint);
        }
    }

    private void drawColumn(float[] yAxisValues) {
        Paint paint = new Paint();
        paint.setStrokeWidth(1);
        paint.setColor(Color.BLUE);

        for (int i = 0; i < yAxisValues.length; i++) {
            int columnWid = getColumnWid(yAxisValues[i]);
            if (columnWid == 0)
                columnWid = 5;
            if (mColumnColors != null)
                paint.setColor(Color.parseColor(mColumnColors[i]));

            Rect rect = new Rect((int) columnStartPos, (int) (mColumnHei + mColumnInterval) * i + mPadding / 2, (int) (columnStartPos + columnWid), (int) mColumnHei + (int) (mColumnHei + mColumnInterval) * i + mPadding / 2);
            mCanvas.drawRect(rect, paint);
        }
    }

    public int getColumnWid(float yAxisValue) {
//        return (int) (yAxisValue * mCanvas.getWidth() / (xMaxValue - xMinValue));
        return (int) (mXInterval * yAxisValue / stepSize);
    }


}
